package com.petalsgroup.petals.service.animation.impl;

import com.petalsgroup.petals.dao.Animation.AnimationDao;
import com.petalsgroup.petals.dao.Animation.CategoryDao;
import com.petalsgroup.petals.dao.Animation.impl.AnimationDaoImpl;
import com.petalsgroup.petals.dao.Animation.impl.CategoryDaoImpl;
import com.petalsgroup.petals.pojo.CategoryDO;
import com.petalsgroup.petals.service.animation.AnimationService;
import com.petalsgroup.petals.service.animation.DirectoryService;
import com.petalsgroup.petals.util.JedisUtil;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class DirectoryServiceImpl implements DirectoryService {
    private CategoryDao categoryDao = new CategoryDaoImpl();
    private AnimationDao animationDao = new AnimationDaoImpl();

    @Override
    public List<CategoryDO> listCategory() {
        List<CategoryDO> list = null;
//        Jedis jedis = JedisUtil.getJedis();
//        Set<Tuple> category = jedis.zrangeWithScores("Category",0,-1);
//        if (category != null && category.size() > 0) {
//            // redis中存在
//            list = new ArrayList<>();
//            for (Tuple tuple : category) {
//                System.out.println("查询了Redis数据库");
//                CategoryDO categoryDO = new CategoryDO();
//                categoryDO.setCid((int) tuple.getScore());
//                categoryDO.setCname(tuple.getElement());
//                list.add(categoryDO);
//            }
//        } else {
//            // redis中不存在
//            System.out.println("查询了MySql数据库");
            list = categoryDao.listCategory();
//            if (list != null && list.size() > 0) {
//                for (CategoryDO categoryDO : list) {
//                    jedis.zadd("Category", categoryDO.getCid(), categoryDO.getCname());
//                }
//            }
//        }
        return list;
    }

    @Override
    public List<Integer> listYear() {
        List<Integer> list = null;

//        Jedis jedis = JedisUtil.getJedis();
//        List<String> yearList = jedis.lrange("yearList", 0, -1);
//        if (yearList != null && yearList.size() > 0) {
//            // redis中存在
//            System.out.println("查询了Redis数据库");
//            list = new ArrayList<>(yearList.size());
//            for (String s : yearList) {
//                list.add(Integer.valueOf(s));
//            }
//        } else {
//            // redis中不存在
//            System.out.println("查询了MySql数据库");
            list = animationDao.listAllYear();
//            for (Integer year : list) {
//                jedis.lpush("yearList", String.valueOf(year));
//            }
//        }

        return list;
    }
}
